package cn.itcast.helloworld;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class twoActivity extends AppCompatActivity implements View.OnClickListener {
    MyHelper myHelper;
    private EditText mEtName;
    private EditText mEtPhone;
    private Button myAdd;
    private Button myCancel;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout1);
        myHelper = new MyHelper(this);
        init();
    }
    private void init(){
        mEtName = (EditText) findViewById(R.id.et_name);
        mEtPhone = (EditText) findViewById(R.id.et_phone);
        myAdd = (Button) findViewById(R.id.btn1_add);
        myCancel = (Button) findViewById(R.id.btn_cancel);
        mEtName.setOnClickListener(this);
        mEtPhone.setOnClickListener(this);
        myAdd.setOnClickListener(this);
        myCancel.setOnClickListener(this);
    }
    public void onClick(View v) {
        String name;
        String phone;
        ContentValues values;
        SQLiteDatabase db;
        switch(v.getId()){
            case R.id.btn1_add:
                name = mEtName.getText().toString();
                phone = mEtPhone.getText().toString();
                db = myHelper.getReadableDatabase();
                values = new ContentValues();
                values.put("name",name);
                values.put("phone",phone);
                db.insert("information" ,null,values);
                Toast.makeText(this,"信息已添加",Toast.LENGTH_SHORT).show();
                db.close();
                finish();
                break;
            case R.id.btn_cancel:
                finish();
        }
    }
}